package drds.global_transaction.api.rpc.config;

import drds.global_transaction.api.rpc.rpc_client.Role;
import lombok.Getter;
import lombok.Setter;

public class ClientConfig extends Config
{

	private static final int per_host_min_conn = 2;
	private static final int rpc_request_timeout = 30 * 1000;
	private static final int max_not_writeable_retry = 2000;
	private static final int max_check_alive_retry = 300;
	private static final int check_alive_internal = 10;
	private static final String socket_address_start_char = "/";
	private static final int default_selector_thread_size = 1;
	private static final long max_acquire_conn_mills = 60 * 1000l;
	private static final String default_selector_thread_prefix = "nettyclientselector";
	private static final String default_worker_thread_prefix = "nettyclientworkerthread";
	private static final String rpc_dispatch_thread_prefix = "rpcdispatch";
	private static final int default_max_pool_active = 1;
	private static final int default_min_pool_idle = 0;
	private static final boolean default_pool_test_borrow = true;
	private static final boolean default_pool_test_return = true;
	private static final boolean default_pool_fifo = true;
	private static String vgroup;
	private static String applicationId;
	private static int clientType;
	private static int maxInactiveChannelCheck = 10;

	@Setter
	@Getter
	private boolean useConnPool = false;
	@Setter
	@Getter
	private int connectTimeoutMillis = 10000;
	@Setter
	@Getter
	private int clientSocketSndBufSize = 153600;
	@Setter
	@Getter
	private int clientSocketRcvBufSize = 153600;
	@Setter
	@Getter
	private int clientWorkerThreads = worker_thread_size;
	@Setter
	@Getter
	private int perHostMaxConn = 2;
	@Setter
	@Getter
	private int pendingConnSize = Integer.MAX_VALUE;

	public static int getRpcRequestTimeout()
	{
		return rpc_request_timeout;
	}

	public static void setVgroup(String vgroup)
	{
		ClientConfig.vgroup = vgroup;
	}

	public static void setApplicationId(String applicationId)
	{
		ClientConfig.applicationId = applicationId;
	}

	public static void setClientType(int clientType)
	{
		ClientConfig.clientType = clientType;
	}

	public static int getMaxNotWriteableRetry()
	{
		return max_not_writeable_retry;
	}

	public static int getPerHostMinConn()
	{
		return per_host_min_conn;
	}

	public static int getMaxCheckAliveRetry()
	{
		return max_check_alive_retry;
	}

	public static int getCheckAliveInternal()
	{
		return check_alive_internal;
	}

	public static String getSocketAddressStartChar()
	{
		return socket_address_start_char;
	}

	public int getChannelMaxWriteIdleSeconds()
	{
		return max_write_idle_seconds;
	}

	public int getChannelMaxReadIdleSeconds()
	{
		return max_read_idle_seconds;
	}

	public int getChannelMaxAllIdleSeconds()
	{
		return max_all_idle_seconds;
	}

	public void setPerHostMaxConn(int perHostMaxConn)
	{
		if (perHostMaxConn > per_host_min_conn)
		{
			this.perHostMaxConn = perHostMaxConn;
		} else
		{
			this.perHostMaxConn = per_host_min_conn;
		}
	}

	public int getClientSelectorThreadSize()
	{
		return CONFIG.getInt("transport.thread-factory.client-selector-thread-size", default_selector_thread_size);
	}

	public long getMaxAcquireConnMills()
	{
		return max_acquire_conn_mills;
	}

	public String getClientSelectorThreadPrefix()
	{
		return CONFIG.getConfig("transport.thread-factory.client-selector-thread-prefix", default_selector_thread_prefix);
	}

	public String getClientWorkerThreadPrefix()
	{
		return CONFIG.getConfig("transport.thread-factory.client-worker-thread-prefix", default_worker_thread_prefix);
	}

	public String getRpcDispatchThreadPrefix()
	{
		return rpc_dispatch_thread_prefix;
	}

	public int getMaxPoolActive()
	{
		return default_max_pool_active;
	}

	public int getMinPoolIdle()
	{
		return default_min_pool_idle;
	}

	public boolean isPoolTestBorrow()
	{
		return default_pool_test_borrow;
	}

	public boolean isPoolTestReturn()
	{
		return default_pool_test_return;
	}

	public boolean isPoolFifo()
	{
		return default_pool_fifo;
	}

	public String getTmDispatchThreadPrefix()
	{
		return rpc_dispatch_thread_prefix + "_" + Role.transaction_manager_client.name();
	}

	public String getRmDispatchThreadPrefix()
	{
		return rpc_dispatch_thread_prefix + "_" + Role.resource_manager_client.name();
	}
}
